//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package org.jpgolay.me.statistic;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.*;
import org.jpgolay.gui.date.DateComboBox;
import org.jpgolay.gui.fixed.*;
import org.jpgolay.me.util.MEDate;
import org.jpgolay.me.util.Parameter;

// Referenced classes of package org.jpgolay.me.statistic:
//            StatisticsWPCRUD, StatisticsWelcomePiecesGUI
public class StatisticsWPController {

    class RemindTask extends TimerTask {

        public void run() {
            actionSearch();
        }

        RemindTask() {
        }
    }

    public StatisticsWPController(StatisticsWelcomePiecesGUI statPanel, Parameter param, JDesktopPane desktop) {
        this.statPanel = null;
        this.param = null;
        calendarPeriod = null;
        dateComboBox = null;
        this.statPanel = statPanel;
        this.param = param;
        this.desktop = desktop;
        date = new MEDate();
        crud = new StatisticsWPCRUD();
        timer = new Timer();
        initStatPanel();
    }

    private void initStatPanel() {
        statPanel.getStatPeriodDate().setLabelLeft("P\351riode : MOIS EN COURS");
        statPanel.getStatInfoLine1().setLabelLeft("BL non livr\351");
        statPanel.getStatInfoLine1().setLabelRight("0");
        statPanel.getStatInfoLine2().setLabelLeft("BL non factur\351");
        statPanel.getStatInfoLine2().setLabelRight("0");
        statPanel.getStatInfoLine3().setLabelLeft("Facture(s) non sold\351e(s)");
        statPanel.getStatInfoLine3().setLabelRight("0");
        statPanel.getStatInfoLine4().setLabelLeft("Facture(s) hors \351ch\351ance");
        statPanel.getStatInfoLine4().setLabelRight("0");
        statPanel.getStatInfoLine5().setLabelLeft("Bon de commande \340 pr\351parer");
        statPanel.getStatInfoLine5().setLabelRight("0");
        statPanel.getStatPeriodDate().getButtonRight().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionCalendarPeriod();
            }
        });
        actionSearch();
    }

    private CalendarPeriod getCalendarPeriod() {
        if (calendarPeriod == null) {
            calendarPeriod = new CalendarPeriod();
            calendarPeriod.setSelectedPeriod(2);
            calendarPeriod.getButtonLeft().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    actionCalendarPeriodOK();
                }
            });
            calendarPeriod.getButtonRight().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    actionCalendarPeriodCANCEL();
                }
            });
            calendarPeriod.setLocation(600, 50);
            calendarPeriod.setSize(320, 130);
        }
        return calendarPeriod;
    }

    private void actionSearch() {
        String from = getCalendarPeriod().getFromDate();
        String to = getCalendarPeriod().getToDate();
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT id FROM ventes WHERE prefixe='BL' AND date_livraison='0000-00-00'");
        sql.append(" AND date BETWEEN '");
        sql.append(date.toMysql(from));
        sql.append("' AND '");
        sql.append(date.toMysql(to));
        sql.append("' ORDER BY id");
        Object data[][] = crud.getDataList(sql.toString());
        if (data != null) {
            statPanel.getStatInfoLine1().setLabelRight(Integer.toString(data.length));
        } else {
            statPanel.getStatInfoLine1().setLabelRight("0");
        }
        sql = new StringBuffer();
        sql.append("SELECT id FROM ventes WHERE prefixe='BL' AND no_fact IS NULL");
        sql.append(" AND date BETWEEN '");
        sql.append(date.toMysql(from));
        sql.append("' AND '");
        sql.append(date.toMysql(to));
        sql.append("' ORDER BY id");
        data = crud.getDataList(sql.toString());
        if (data != null) {
            statPanel.getStatInfoLine2().setLabelRight(Integer.toString(data.length));
        } else {
            statPanel.getStatInfoLine2().setLabelRight("0");
        }
        sql = new StringBuffer();
        sql.append("SELECT id FROM ventes_factures WHERE prefixe='F' AND solde_le='0000-00-00'");
        sql.append(" AND date BETWEEN '");
        sql.append(date.toMysql(from));
        sql.append("' AND '");
        sql.append(date.toMysql(to));
        sql.append("' ORDER BY id");
        data = crud.getDataList(sql.toString());
        if (data != null) {
            statPanel.getStatInfoLine3().setLabelRight(Integer.toString(data.length));
        } else {
            statPanel.getStatInfoLine3().setLabelRight("0");
        }
        sql = new StringBuffer();
        sql.append("SELECT id FROM ventes_factures WHERE prefixe='F' AND echeance<'" + date.getMysql() + "' AND solde_le='0000-00-00'");
        sql.append(" AND date BETWEEN '");
        sql.append(date.toMysql(from));
        sql.append("' AND '");
        sql.append(date.toMysql(to));
        sql.append("' ORDER BY id");
        data = crud.getDataList(sql.toString());
        if (data != null) {
            statPanel.getStatInfoLine4().setLabelRight(Integer.toString(data.length));
        } else {
            statPanel.getStatInfoLine4().setLabelRight("0");
        }
        sql = new StringBuffer();
        sql.append("SELECT id FROM ventes WHERE prefixe='C'");
        sql.append(" AND date BETWEEN '");
        sql.append(date.toMysql(from));
        sql.append("' AND '");
        sql.append(date.toMysql(to));
        sql.append("' ORDER BY id");
        data = crud.getDataList(sql.toString());
        if (data != null) {
            statPanel.getStatInfoLine5().setLabelRight(Integer.toString(data.length));
        } else {
            statPanel.getStatInfoLine5().setLabelRight("0");
        }
        timer.schedule(new RemindTask(), 60000L);
    }

    private void actionCalendarPeriod() {
        getCalendarPeriod().setVisible(true);
        getCalendarPeriod().toFront();
        desktop.add(getCalendarPeriod(), JLayeredPane.DEFAULT_LAYER);
        try {
            getCalendarPeriod().setSelected(true);
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
    }

    private void actionCalendarPeriodOK() {
        statPanel.getStatPeriodDate().setLabelLeft(getCalendarPeriod().getMessage());
        actionSearch();
        actionCalendarPeriodCANCEL();
    }

    private void actionCalendarPeriodCANCEL() {
        getCalendarPeriod().setVisible(false);
        desktop.remove(getCalendarPeriod());
    }
    private StatisticsWelcomePiecesGUI statPanel;
    private Parameter param;
    private CalendarPeriod calendarPeriod;
    private StatisticsWPCRUD crud;
    private JDesktopPane desktop;
    private Timer timer;
    private DateComboBox dateComboBox;
    private MEDate date;
}
